package com.example.newhbsj;

import java.util.ArrayList;
import java.util.List;


import com.example.bean.Member;
import com.example.dao.Dbhelp;
import com.example.newhbsj.R;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class SetpasswdActivity extends Activity{
	
	private ImageButton check2;
	private EditText newpasswd;
	private Boolean ii=false;
	private Button success;
	private ImageButton exit;
	private Dbhelp dbhelp;
	 List<Member> listMembers =new  ArrayList<Member>(); 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setpasswd);
		dbhelp=new Dbhelp(this, "users.db", null, 1);
		
		newpasswd=(EditText) findViewById(R.id.newpasswd1);
		check2=(ImageButton) findViewById(R.id.check2);
		success=(Button) findViewById(R.id.success);
		exit=(ImageButton) findViewById(R.id.exit);
		
		
		exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			finish();	
			Intent i=new Intent(SetpasswdActivity.this,RegisterActivity.class);
			startActivity(i);
			}
		});
		
		
		
		
		
		check2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			if(ii){
				ii=false;
				newpasswd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			}	else{
				ii=true;
				newpasswd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
			Editable e=newpasswd.getText();
			Selection.setSelection(e, e.length());	
			}
		});
		
		success.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
//				SharedPreferences sp=getSharedPreferences("data", MODE_PRIVATE);
//				SharedPreferences.Editor edit=getSharedPreferences("data",MODE_PRIVATE).edit();
				SQLiteDatabase db=dbhelp.getWritableDatabase();
				ContentValues values=new ContentValues();
				String account=RegisterActivity.tel1.toString();
				String passwd=newpasswd.getText().toString();
				if(length(passwd)){
					RegisterActivity.activity.finish();
					LogonActivity.mainactivity.finish();
					finish();
					values.put("account", account);
					values.put("passwd", passwd);
					
					db.insert("users", null, values);
					values.clear();
					db.close();
					
					
					
					
////					edit.putString("account", account);
////					edit.putString("passwd", passwd);
////					edit.commit();
				
				
//				String account=RegisterActivity.tel1.toString();
//				String passwd=newpasswd.getText().toString();
//				String members =sp.getString("member", "");   //������xml�е��û���Ϣ�ַ�
//			
//		        	
//		            String users = account+"/"+passwd+",";
//		            
//		                members += users;
//		                System.out.println(members);  
//		        edit.putString("members", members);
//		        edit.commit();
				
				
		        
		        
		        
					finish();
					Intent i=new Intent(SetpasswdActivity.this,LogonActivity.class);
					startActivity(i);
				}else{
					Toast.makeText(SetpasswdActivity.this, "请输入6-16位字符", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		
		
		
		findViewById(R.id.set).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {  
			    case R.id.set:  
			         InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
			         imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  
			        break;  
			}
		}
		});
		
	}
	
	private boolean length(String str){
		boolean b=false;
		if(str.length()>5&&str.length()<17){
			b=true;
		}
		return b;
		
	}
	
	
}
